package com.engine.kq.biz;

import weaver.cache.*;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;

public class KQShiftScheduleComInfo extends CacheBase{

	protected static String TABLE_NAME = "kq_shiftschedule";
	/** sql中的where信息，不要以where开始 */
	protected static String TABLE_WHERE = "where (isdelete is null or  isdelete <> '1') ";//默认加载今天之前一个月数据
	/** sql中的order by信息，不要以order by开始 */
	protected static String TABLE_ORDER = "id";

	@PKColumn(type = CacheColumnType.NUMBER)
	protected static String PK_NAME = "id";

	@CacheColumn(name = "serialid")
	protected static int serialid;

	@CacheColumn(name = "groupid")
	protected static int groupid;

  private boolean isFormat = false;
  private int shiftGroupId = -1;

	@Override
	public CacheMap initCache() throws Exception {
		CacheMap localData = super.createCacheMap();
		RecordSet rs = new RecordSet();
		String sql = "";
		if ("mysql".equals(rs.getDBType())) {
			sql += " select CONCAT(resourceid,'|',kqdate) as id, serialid,groupid from kq_shiftschedule "+TABLE_WHERE + " and kqdate>='"+DateUtil.getFirstDayOfMonth(DateUtil.getCurrentDate())+"' and kqdate<='"+ DateUtil.getCurrentDate()+"'";
		}else if("sqlserver".equals(rs.getDBType())){
			sql += " select CONVERT(varchar,resourceid)+'|'+kqdate as id, serialid,groupid from kq_shiftschedule "+TABLE_WHERE + " and kqdate>='"+DateUtil.getFirstDayOfMonth(DateUtil.getCurrentDate())+"' and kqdate<='"+ DateUtil.getCurrentDate()+"'";
		} else {
			sql = "select resourceid||'|'||kqdate as id, serialid,groupid from kq_shiftschedule "+TABLE_WHERE + " and kqdate>='"+DateUtil.getFirstDayOfMonth(DateUtil.getCurrentDate())+"' and kqdate<='"+ DateUtil.getCurrentDate()+"'";
		}

		sql += " and (isdelete is null or  isdelete <> '1')";
		rs.executeQuery(sql);
		while (rs.next()) {
			String id = Util.null2String(rs.getString(PK_NAME));
			CacheItem row = createCacheItem();
			parseResultSetToCacheItem(rs, row);

			// 这里需要记得调用这个 转化方法， 否则 这些自己加载的数据就不能正确转化，当然你也可以直接在转化逻辑写在这里，不过这样先让代码重复了
			modifyCacheItem(id, row);
			localData.put(id, row);
		}
		return localData;
	}

//	/**
//	 * 性能原因取消后续加载
//	 * @return
//	 */
//	protected boolean autoInitIfNotFound() {
//		return false;
//	}

	@Override
	public CacheItem initCache(String key) {
		if (key == null || "".equals(key.trim())) {
			return null;
		}

		String[] keys = Util.splitString(key,"|");
		String resourceid = keys[0];
		String kqdate = keys[1];
		RecordSet rs = new RecordSet();
		String sql = "";
		if ("mysql".equals(rs.getDBType())) {
			sql += " select CONCAT(resourceid,'|',kqdate) as id, serialid,groupid from kq_shiftschedule  "+TABLE_WHERE+ " and resourceid ='"+resourceid+"' and kqdate='"+kqdate+"'";
		}else if("sqlserver".equals(rs.getDBType())){
			sql += " select CONVERT(varchar,resourceid)+'|'+kqdate as id, serialid,groupid from kq_shiftschedule  "+TABLE_WHERE+ " and resourceid ='"+resourceid+"' and kqdate='"+kqdate+"'";
		} else {
			sql = "select resourceid||'|'||kqdate as id, serialid,groupid from kq_shiftschedule "+TABLE_WHERE+ " and resourceid ='"+resourceid+"' and kqdate='"+kqdate+"'";
		}

		sql += " and (isdelete is null or  isdelete <> '1')";
		rs.executeQuery(sql);
		if (rs.next()) {
			CacheItem cacheItem = createCacheItem();
			parseResultSetToCacheItem(rs, cacheItem);
			modifyCacheItem(key, cacheItem);
			return cacheItem;
		}
		return null;
	}

	private String getId() {
		return (String) getRowValue(PK_INDEX);
	}

	private String getSerialId() {
		return (String) getRowValue(serialid);
	}

	private String getSerialId(String key) {
    if(!this.isFormat){
      return (String) getValue(serialid, key);
    }
		return getSerialidBySql(key);
	}

  private String getSerialidBySql(String key) {

    String sql_serialid = "";
    if (key == null || "".equals(key.trim())) {
      return null;
    }
    String[] keys = Util.splitString(key,"|");
    String resourceid = keys[0];
    String kqdate = keys[1];
    RecordSet rs = new RecordSet();
    String sql = "";
    if ("mysql".equals(rs.getDBType())) {
      sql += " select CONCAT(resourceid,'|',kqdate) as id, serialid,groupid from kq_shiftschedule  where (isdelete is null or  isdelete <> '1') and resourceid ='"+resourceid+"' and kqdate='"+kqdate+"'";
    }else if("sqlserver".equals(rs.getDBType())){
      sql += " select CONVERT(varchar,resourceid)+'|'+kqdate as id, serialid,groupid from kq_shiftschedule  where (isdelete is null or  isdelete <> '1') and resourceid ='"+resourceid+"' and kqdate='"+kqdate+"'";
    } else {
      sql = "select resourceid||'|'||kqdate as id, serialid,groupid from kq_shiftschedule where (isdelete is null or  isdelete <> '1') and resourceid ='"+resourceid+"' and kqdate='"+kqdate+"'";
    }

	if(shiftGroupId > 0) {
		sql += " and groupid = "+shiftGroupId;
	}
    sql += " and (isdelete is null or  isdelete <> '1')";
    rs.executeQuery(sql);
    if (rs.next()) {
      sql_serialid = rs.getString("serialid");
    }
    return sql_serialid;
  }

	private String getGroupId(String key) {
		return (String) getValue(groupid, key);
	}

	public String getSerialId(String resourceid, String kqdate) {
		if(Util.null2String(resourceid).length()==0||Util.null2String(kqdate).length()==0)return "";
		return this.getSerialId(resourceid+"|"+kqdate);
	}

	public String getGroupId(String resourceid, String kqdate) {
		if(Util.null2String(resourceid).length()==0||Util.null2String(kqdate).length()==0)return "";
		return this.getGroupId(resourceid+"|"+kqdate);
	}

  public boolean isFormat() {
    return isFormat;
  }

  public void setFormat(boolean format) {
    isFormat = format;
  }

	public int getShiftGroupId() {
		return shiftGroupId;
	}

	public void setShiftGroupId(int shiftGroupId) {
		this.shiftGroupId = shiftGroupId;
	}
}